/*package com.distribute.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.CookieUtils;
import com.distribute.common.util.HttpClientUtil;
import com.distribute.common.util.JsonUtils;
import com.distribute.pojo.DbItem;
import com.distribute.pojo.DbUser;
import com.distribute.portal.dao.JedisClient;
import com.distribute.portal.pojo.CartItem;
import com.distribute.portal.service.CartService;

*//**
 * @version 1.0
 *//*
@Service
public class CartServiceImpl implements CartService {

	@Autowired
	JedisClient jedisClient;
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${ITME_INFO_URL}")
	private String ITME_INFO_URL;

	@Override
	public DistributeResult addCartItem(long itemId, int num, HttpServletRequest request,
			HttpServletResponse response) {
		// 不能用car+uuid因为每次登陆uuid是会变化的所以用不变的就是username
		String userinfo = jedisClient.get(CookieUtils.getCookieValue(request, "TT_TOKEN"));
		// 获取用户的名字
		String key = "cart-" + JsonUtils.jsonToPojo(userinfo, DbUser.class).getUsername();
		// 判断是否有购物车
		if (jedisClient.get(key) != null) {
			// 若果存在的话 判断集合中是否存在该商品
			String redisResult = jedisClient.get(key);
			List<DbItem> list = JsonUtils.jsonToList(redisResult, DbItem.class);
			boolean isExist = false;
			// 循环判断改id是否存在如果存在的话num+1
			for (DbItem item : list) {
				if (item.getId() == itemId) {
					isExist = true;
					item.setNum(item.getNum() + 1);
					break;
				}
			}
			// 如果购物车存在但是id不存在就重新添加一个并重新set进去
			if (!isExist) {
				Map<String, String> map = new HashMap<>();
				map.put("id", id + "");
				String result = HttpClientUtil.doPost(url, map);
				TbItemImages tbItem = JsonUtils.jsonToPojo(result, TbItemImages.class);
				// 第一次创建购物车时将物品设置为1
				tbItem.setNum(1);
				tbItem.setImages(
						tbItem.getImages() == null || "".equals("") ? new String[0] : tbItem.getImage().split(","));
				list.add(tbItem);
			}
			dao.set(key, JsonUtils.objectToJson(list));
			return list;
		} else
		// 没有key表示没有购物车 将购物车装到redis中
		{
			Map<String, String> map = new HashMap<>();
			map.put("id", id + "");
			String result = HttpClientUtil.doPost(url, map);
			TbItemImages tbItem = JsonUtils.jsonToPojo(result, TbItemImages.class);
			// 第一次创建购物车时将物品设置为1
			tbItem.setNum(1);
			tbItem.setImages(
					tbItem.getImages() == null || "".equals("") ? new String[0] : tbItem.getImage().split(","));
			List<TbItemImages> list = new ArrayList<>();
			list.add(tbItem);
			dao.set(key, JsonUtils.objectToJson(list));
			return list;
		}
	}

	}

	@Override
	public List<CartItem> getCartItemList(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DistributeResult deleteCartItem(long itemId, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

		*//**
			 * 添加购物车商品
			 * <p>
			 * Title: addCartItem
			 * </p>
			 * <p>
			 * Description:
			 * </p>
			 * 
			 * @param itemId
			 * @param num
			 * @return
			 * @see com.taotao.portal.service.CartService#addCartItem(long, int)
			 *//*
	
	 * @Override public DistributeResult addCartItem(long itemId, int num,
	 * HttpServletRequest request, HttpServletResponse response) { //取商品信息 CartItem
	 * cartItem = null; //取购物车商品列表 List<CartItem> itemList =
	 * getCartItemList(request); //判断购物车商品列表中是否存在此商品 for (CartItem cItem : itemList)
	 * { //如果存在此商品 if (cItem.getId() == itemId) { //增加商品数量
	 * cItem.setNum(cItem.getNum() + num); cartItem = cItem; break; } } if (cartItem
	 * == null) { cartItem = new CartItem(); //根据商品id查询商品基本信息。 String json =
	 * HttpClientUtil.doGet(REST_BASE_URL + ITME_INFO_URL + itemId);
	 * //把json转换成java对象 DistributeResult distributeResult =
	 * DistributeResult.formatToPojo(json, DbItem.class); if
	 * (distributeResult.getStatus() == 200) { DbItem item = (DbItem)
	 * distributeResult.getData(); cartItem.setId(item.getId());
	 * cartItem.setTitle(item.getTitle()); cartItem.setImage(item.getImage() == null
	 * ? "":item.getImage().split(",")[0]); cartItem.setNum(num);
	 * cartItem.setPrice(item.getPrice()); } //添加到购物车列表 itemList.add(cartItem); }
	 * //把购物车列表写入cookie CookieUtils.setCookie(request, response, "TT_CART",
	 * JsonUtils.objectToJson(itemList), true);
	 * 
	 * return DistributeResult.ok(); }
	 *//**
		 * 从cookie中取商品列表
		 * <p>
		 * Title: getCartItemList
		 * </p>
		 * <p>
		 * Description:
		 * </p>
		 * 
		 * @return
		 *//*
	
	 * private List<CartItem> getCartItemList(HttpServletRequest request) {
	 * //从cookie中取商品列表 String cartJson = CookieUtils.getCookieValue(request,
	 * "TT_CART", true); if (cartJson == null) { return new ArrayList<>(); }
	 * //把json转换成商品列表 try { List<CartItem> list = JsonUtils.jsonToList(cartJson,
	 * CartItem.class); return list; } catch (Exception e) { e.printStackTrace(); }
	 * return new ArrayList<>(); }
	 * 
	 * 
	 * @Override public List<CartItem> getCartItemList(HttpServletRequest request,
	 * HttpServletResponse response) { List<CartItem> itemList =
	 * getCartItemList(request); return itemList; }
	 *//**
		 * 删除购物车商品
		 * <p>
		 * Title: deleteCartItem
		 * </p>
		 * <p>
		 * Description:
		 * </p>
		 * 
		 * @param itemId
		 * @return
		 * @see com.taotao.portal.service.CartService#deleteCartItem(long)
		 *//*
			 * @Override public DistributeResult deleteCartItem(long itemId,
			 * HttpServletRequest request, HttpServletResponse response) {
			 * //从cookie中取购物车商品列表 List<CartItem> itemList = getCartItemList(request);
			 * //从列表中找到此商品 for (CartItem cartItem : itemList) { if (cartItem.getId() ==
			 * itemId) { itemList.remove(cartItem); break; }
			 * 
			 * } //把购物车列表重新写入cookie CookieUtils.setCookie(request, response, "TT_CART",
			 * JsonUtils.objectToJson(itemList), true);
			 * 
			 * return DistributeResult.ok(); }
			 * 
			 

}
*/